<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="日期范围">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" :allow-clear="false" />
        </a-form-item>
        <a-form-item name="pyjm" label="" class="ml-4">
          <a-input v-model:value="formState.pyjm" class="w-64" placeholder="IP/员工编号" allow-clear @keydown.enter="getData" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-start">
        <a-button class="mb-2" type="primary" :loading="signInLoading" @click="onSinIn"> 本机签到 </a-button>
        <a-button class="mb-2 ml-2" type="primary" :loading="signOutLoading" @click="onSinOut"> 本机签退 </a-button>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" column-code="000563" :pagination="pagination">
        <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import useUserStore from '@/store/user'
import { post } from '@/utils/request'

const { empId } = useUserStore()
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 3)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  pyjm: ''
})
const signInLoading = ref(false)
const signOutLoading = ref(false)
const ipMac = ref({
  ip: '',
  mac: ''
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/materials/getNmiSignLogEntityList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      startDate: formState.value.dateRange[0],
      endDate: formState.value.dateRange[1],
      pyjm: params.pyjm
    }
  }
})

const onSinIn = async () => {
  signInLoading.value = true
  const { code, msg } = await post('/outp/materials/dosignln', {
    ip: ipMac.value.ip,
    mac: ipMac.value.mac,
    opter_no: empId
  })
  if (code !== 0) {
    message.error(msg || '签到失败')
    signInLoading.value = false
    return
  }
  getData()
  message.success('签到成功')
  signInLoading.value = false
}
const onSinOut = async () => {
  signOutLoading.value = true
  const { code, msg } = await post('/outp/materials/dosignoff', {
    ip: ipMac.value.ip,
    mac: ipMac.value.mac,
    opter_no: empId
  })
  if (code !== 0) {
    message.error(msg || '签退失败')
    signOutLoading.value = false
    return
  }
  getData()
  message.success('签退成功')
  signOutLoading.value = false
}

onMounted(() => {
  if (window.openExe?.getIPAndMAC()) {
    ipMac.value = JSON.parse(window.openExe?.getIPAndMAC()) || {}
  }
})
</script>
<style></style>
